//
//  QJDetailVCRightImageCell.m
//  QuanJieShopProject
//
//  Created by apple on 2018/2/1.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "QJDetailVCRightImageCell.h"
#import "SDImageCache.h"

@implementation QJDetailVCRightImageCell

-(void)cellUpdateWithImageUrl:(NSString *)url{
    if (!url.length) return;
    
    UIImage *cachedImage = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:url];
    
    if (cachedImage) {

        // 重置frame
        [self configFrameWithImage:cachedImage];
        
        self.cellImage.image = cachedImage;

    }else {
        __weak typeof(self) weakSelf = self;

//        KMyLog(@"==============  网络请求图片");
        [self.cellImage sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"noImage"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            
            if (!error) {
                // 保存图片
                [[SDImageCache sharedImageCache] storeImage:image forKey:url completion:nil];
                KMyLog(@"url = %@", url);
                // 重置frame
                [self configFrameWithImage:image];
                
                if (weakSelf.updateCellHeightBlock) {
                    weakSelf.updateCellHeightBlock();
                }
            }
            
        }];
        
    }


}

// 根据图片计算frame
- (void)configFrameWithImage:(UIImage *)image
{
    if (!image.size.width) return;
    
    CGFloat height = image.size.height *UISCREEN_WIDTH/image.size.width;
    [self sizeThatFits:CGSizeMake(UISCREEN_WIDTH, height)];
    
    self.cellHeight = image.size.height / image.size.width * UISCREEN_WIDTH;
    
    self.cellImage.frame = CGRectMake(0, 0, UISCREEN_WIDTH, height);
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
